package com.qczl.pme.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qczl.pme.dao.CameraDao;
import com.qczl.pme.dao.LiveDao;
import com.qczl.pme.dao.LiveVideoDao;
import com.qczl.pme.dao.MettingDao;
import com.qczl.pme.dao.PersonDao;
import com.qczl.pme.dao.SignDao;
import com.qczl.pme.dao.StbDao;
import com.qczl.pme.dao.VideoDao;
import com.qczl.pme.entity.PMECamera;
import com.qczl.pme.entity.PMELive;
import com.qczl.pme.entity.PMELiveVideo;
import com.qczl.pme.entity.PMEMetting;
import com.qczl.pme.entity.PMEPerson;
import com.qczl.pme.entity.PMESTB;
import com.qczl.pme.entity.PMESign;
import com.qczl.pme.entity.PMEVideo;
import com.qczl.pme.service.VideoCallBackService;
import com.qczl.util.DateUtil;

/**
 * sewise回调业务逻辑层实现类
 * 
 * @author SongR
 *
 */
@Service
public class VideoCallBackServiceImpl implements VideoCallBackService {

	@Autowired
	SignDao signdao;
	@Autowired
	PersonDao persondao;
	@Autowired
	VideoDao videodao;
	@Autowired
	StbDao stbdao;
	@Autowired
	CameraDao cameradao;
	@Autowired
	LiveVideoDao lvdao;
	@Autowired
	LiveDao livedao;
	@Autowired
	MettingDao mettingdao;

	/**
	 * 实现保存录制视频信息
	 */
	@Override
	public void callbackZhucun(PMEVideo video) throws Exception {
		PMESign sign = signdao.get("from PMESign where taskid='" + video.getTaskId() + "'");
		PMEPerson person = persondao.get(PMEPerson.class, sign.getPersonid());
		PMESTB stb = stbdao.get("from PMESTB where no ='" + sign.getStbno() + "'");
		PMECamera camera = cameradao.get(PMECamera.class, stb.getCameraid());
		video.setPlayUrl(
				"http://" + camera.getSewise().getIp() + ":" + camera.getSewise().getProt2() + video.getPlayUrl());
		video.setDownloadUrl(
				"http://" + camera.getSewise().getIp() + ":" + camera.getSewise().getProt2() + video.getDownloadUrl());
		video.setCreatTime(new Date());
		video.setName("驻村干部签到" + person.getName());
		Long end = System.currentTimeMillis();
		Long star = end - video.getDuration() * 1000;
		video.setEndTime(DateUtil.MillisToDate(end));
		video.setStartTime(DateUtil.MillisToDate(star));
		video.setStatus(0);
		video.setCamera(camera);
		videodao.save(video);

	}

	/**
	 * 保存直播录制
	 */
	@Override
	public void callBackLiveV1(String taskid, String download_url, String play_url, Integer duration) throws Exception {
		PMELiveVideo lv = lvdao.get("from PMELiveVideo where taskId='" + taskid + "'");
		lv.setDownloadUrl(download_url);
		lv.setPlayUrl(play_url);
		lv.setDuration(new Long(duration));
		lv.setEndTime(new Date());
		lvdao.saveOrUpdate(lv);
		PMELive live = livedao.get("from PMELive where videoId=" + lv.getId());
		live.setStatus(0);
		livedao.update(live);
	}

	@Override
	public void callBackMettingV1(String taskid, String download_url, String play_url, Integer duration)
			throws Exception {
		PMEVideo video = videodao.get("from PMEVideo where taskId='" + taskid + "'");
		video.setDownloadUrl(download_url);
		video.setPlayUrl(play_url);
		video.setDuration(new Long(duration));
		video.setEndTime(new Date());
		video.setStatus(0);
		videodao.update(video);
		PMEMetting metting = mettingdao.get("from PMEMetting where id=" + video.getMettingId());
		metting.setStatus(0);
		mettingdao.update(metting);
	}

}
